package com.laolu.icon.utils;

import com.laolu.icon.entity.IconInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Objects;

public class MyIconInfoUtils {
	
	private static final Logger log = LoggerFactory.getLogger(MyIconInfoUtils.class);
	
	public static boolean equalsByTitle(@NotNull IconInfo iconInfo1, @NotNull IconInfo iconInfo2) {
		return Objects.equals(iconInfo1.getTitle(), iconInfo2.getTitle());
	}
	
	public static boolean equalsByFullName(@NotNull IconInfo iconInfo1, @NotNull IconInfo iconInfo2) {
		return Objects.equals(iconInfo1.getFullName(), iconInfo2.getFullName());
	}
	
	public static boolean equalsByTitleAndFullName(@NotNull IconInfo iconInfo1, @NotNull IconInfo iconInfo2) {
		return equalsByTitle(iconInfo1, iconInfo2) && equalsByFullName(iconInfo1, iconInfo2);
	}
	
	public static boolean equalsByTitleAndFullNameAndType(@NotNull IconInfo iconInfo1, @NotNull IconInfo iconInfo2) {
		return equalsByTitleAndFullName(iconInfo1, iconInfo2) && Objects.equals(iconInfo1.getType(), iconInfo2.getType());
	}
	
	public static @NotNull IconInfo copyIconInfo(@NotNull IconInfo iconInfo) {
		
		IconInfo newIconInfo = new IconInfo();
		try {
			BeanUtils.copyProperties(newIconInfo, iconInfo);
		} catch (IllegalAccessException | InvocationTargetException e) {
			log.error("copyIconInfo error", e);
		}
		return newIconInfo;
	}
	
	public static @Nullable IconInfo getIconInfoFromList(@NotNull IconInfo iconInfo,
	                                                     @NotNull List<IconInfo> iconInfoList,
	                                                     boolean validateTitle,
	                                                     boolean validateFullName,
	                                                     boolean validateType) {
		//寻找当前桌面图标列表中是否存在选中的备份图标信息
		for (IconInfo tempIcon : iconInfoList) {
			boolean b1 = !validateTitle || MyIconInfoUtils.equalsByTitle(iconInfo, tempIcon);
			boolean b2 = !validateFullName || MyIconInfoUtils.equalsByFullName(iconInfo, tempIcon);
			boolean b3 = !validateType || Objects.equals(iconInfo.getType(), tempIcon.getType());
			
			if (b1 && b2 && b3) {
				return tempIcon;
			}
		}
		return null;
	}
}
